﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PloobsEngine.Publishers
{
    public class ConcreteObserver : IObserver
    {
        private IList<ISubject> _subList = new List<ISubject>();
        private IObserver _instance = null;

        public ConcreteObserver(IObserver Myself)
        {
            this._instance = Myself;
        }
        
        #region IObserver Members

        /// <summary>
        /// Impl Vazia, O usuario nao usara esta Implementacao
        /// </summary>
        /// <param name="obj"></param>
        public virtual void Recieve(object obj) { throw new Exception("usuario devera usar sua propria implementacao desta funcao"); }
        
        public void DetachFromAllSubjects()
        {
            IList<ISubject> subs = _subList.ToList();

            foreach (ISubject sub in subs)
            {
                sub.Dettach(_instance );
            }

            _subList.Clear();
                        
        }

        #endregion

        #region IObserver Members

        public void AddSubject(ISubject sub)
        {
            _subList.Add(sub);
        }

        public void RemoveSubject(ISubject sub)
        {
            _subList.Remove(sub);
        }

        #endregion
    }
}
